/*
 * Implement the "suggestion" box, to catch user's comments,
 * and also a simple tracking mechanism, to send a blip of
 * mail from the first-time user.  This is akin to releasing
 * helium balloons with postcards that say "If you find me,
 * send this card back to . . ." -- it should be interesting
 * to the community to track the free flow of software on
 * the ethernet.
 *
 * If you have any concerns about this, please check with me.
 *
 * Michael Hawley
 * mike@media-lab.mit.edu
 */

#define _NAME "Opener"

static char *stripnl(char *s){
    char *p;
    for (p=s;*p;p++) if (*p == '\n' || *p == '\r') *p = '\0';
    return s;
}

char *
execstr(s) char *s; {
    FILE *f = popen(s,"r");
    char *p = s;
    *s = '\0';
    if (f){
        while (fgets(p,256,f))
            stripnl(p), p += strlen(p);
        pclose(f);
    }
    return s;
}

static void
monitorMail(subj,body,dest) char *subj, *body, *dest; {
    char s[1024], u[256]="whoami", m[256]="hostname", t[4096];
    FILE *f, *popen();
    if (!dest || !*dest) dest = "monitor%tome.media.mit.edu@mit.edu";
    execstr(u); execstr(m);
    if (!body || !*body){
        body = t;
        sprintf(body, "%s@%s\nIf this mail bounces back to you,\n\
could you please correct the destination path if possible,\n\
and send it to monitor@tome.media.mit.edu.\n\
We are trying an experiment to track software.\n",u,m);
    }
    sprintf(s,"/usr/ucb/Mail -s \"%s %s@%s\" %s",
	subj && *subj? subj : [NXApp appName], u, m, dest);
    if (f = popen(s,"w"))
        fprintf(f,"%s",body), pclose(f);
}

static void
OpenerMonitor(subj) char *subj; { // send a note at first use.  to track net.
    extern char FirstUsed[];
    if (!*FirstUsed){
        extern long time();
        extern char *ctime();
        long t = time(0);
        if (t < 793414430) // 1995 
	    monitorMail(subj,0,0);
        strcpy(FirstUsed,ctime(&t));
        stripnl(FirstUsed);
        [_defaults writeDefaults:_defaults];
    }
}

- suggestion:sender {
    char subj[256], w[256] = "whoami";
    char body[4096]="\
Michael:\n\n\
A handy bit of software!  I use it ALL the time.\n\
Now, I know I should be sending you a T shirt, a card, \n\
maybe a tax deductible surprise to further your\n\
student career, indeed I know I could send it to you\n\
at the MIT Media Lab / 20 Ames St / Cambridge, MA 02139\n\
but all I have are these scrimpy electronic comments:\n\n\
   <insert accolades & suggestions here>\n\n\
             parsimoniously,\n\
             ";
#define fcall(a)  [s performRemoteMethod:a]
#define call(a,b) [s performRemoteMethod:a with:b length:strlen(b)+1]
    id s = [NXApp appSpeaker];
    NXPortFromName("Mail", NULL); // make sure app is launched
    [s setSendPort:NXPortFromName("MailSendDemo", NULL)];

    sprintf(subj,"Comments and suggestions for ``%s'' ",_NAME);
    strcat(subj,[version stringValue]);
    strcat(body,execstr(w)); strcat(body,"\n");
    call("setTo:","comments@tome.media.mit.edu");
    call("setSubject:",subj);
    call("setBody:",body);
    
    return self;
}

/*
    OpenerMonitor("Opener-1.0-newuser");
*/
